package com.hqd.jjvm.classformat.attributeinfo;

import com.hqd.jjvm.classformat.ClassFile;
import com.hqd.jjvm.util.HexStrTransformUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 属性信息
 *
 * @author hqd
 */

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class AttributeInfo {
    /**
     * 属性名称
     */
    private Integer attributeNameIndex;
    /**
     * 属性长度
     */
    private Integer attributeLength;
    /**
     * 属性字节
     */
    private byte[] byteCodes;

    public AttributeInfo(Integer attributeNameIndex, Integer attributeLength) {
        this.attributeNameIndex = attributeNameIndex;
        this.attributeLength = attributeLength;
    }

    protected <T extends AttributeInfo> AttributeInfo newAttributeInfo(Integer attributeNameIndex, Integer attributeLength, ClassFile classFile) {
        return this;
    }

    /**
     * 创建一个新属性
     *
     * @param classFile
     * @param <T>
     * @return
     */
    public <T extends AttributeInfo> AttributeInfo newAttributeInfo(ClassFile classFile) {
        this.attributeNameIndex = HexStrTransformUtil.parseHexToInt(classFile.readU2());
        this.attributeLength = HexStrTransformUtil.parseHexToInt(classFile.readU4());
        return newAttributeInfo(attributeNameIndex, attributeLength, classFile);
    }
}
